<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 订单数据
 */
namespace app\allwin\model;
use think\Model;

class AllwinShopOrder extends Model{
    
    protected $pk    = 'id';

    /**
     * @return \think\model\relation\HasOne
     * 商品
     */
    public function shop(){
        return $this->hasOne('AllwinShop','id','shop_id');
    }

    /**
     * @return \think\model\relation\HasOne
     * 店铺
     */
    public function store(){
        return $this->hasOne('AllwinStore','id','store_id');
    }

     /**
     * @return \think\model\relation\hasMany
     * 店铺管理员类别
     */
    public function storeWorker(){
        return $this->hasMany('StoreWorker','store_id','store_id');
    }

    /**
     * @return \think\model\relation\HasOne
     * 用户
     */
    public function user(){
        return $this->hasOne('app\common\model\SystemUser','id','user_id');
    }

    /**
     * @param $data
     * @param $order_no
     * 购买商品生成订单数据
     */
    public static function insertOrder($param,$order_no){
        $order = [
            'store_id'          => $param['store_id'],
            'shop_id'           => $param['shop_id'],
            'member_miniapp_id' => $param['member_miniapp_id'],
            'phone'             => $param['telphone'],
            'message'           => $param['message'],
            'share_uid'         => $param['share_uid']?:0,
            'user_id'           => $param['user_id'],
            'order_no'          => $order_no,
            'points'            => $param['points'],
            'amount'            => $param['amount'],
            'sell_price'        => $param['sell_price'],
            'thrifty'           => $param['thrifty'],
            'shop_cache'        => $param['shop_cache'],
            'status'            => 0,
            'is_del'            => 0,
            'paid_at'           => 0,
            'create_time'       => time()
        ];
        return self::create($order);
    }

    public static function income(array $order,float $allMoney,float $prorateMoney){
        $store = AllwinStore::where(['id' => $order['store_id']])->field('id,member_miniapp_id,charges,manage_uid,mch_id,name')->find();
        if(empty($store)){
            return;
        }
        $setting = AllwinConfig::getConfig($store->member_miniapp_id);
        if($prorateMoney > 0){
            $queen = []; //分账队列参数
            //服务商模式 有商户号 并且开启分帐 钱够分的情况下
            if($setting->is_psp == 1 && $store->mch_id && $setting->is_wechat_profitsharing){
                //分给商户
                $queen[] = [
                    'member_miniapp_id' => $store->member_miniapp_id,
                    'store_id'          => $store->id,
                    'uid'               => 0,
                    'mch_id'            => $store->mch_id,
                    'amount'            => $prorateMoney * 100,
                    'transaction_id'    => $order['paid_no'],
                    'out_order_no'      => $order['order_no'],
                    'is_finish'         => 0,
                    'types'             => 1,
                    'msg'               => '结算到商户号',
                ];
                //分给平台
                $queen[] = [
                    'member_miniapp_id' => $store->member_miniapp_id,
                    'store_id'          => $store->id,
                    'uid'               => 0,
                    'mch_id'            => 0,
                    'amount'            => ($allMoney - $prorateMoney) * 100,
                    'transaction_id'    => $order['paid_no'],
                    'out_order_no'      => $order['order_no'],
                    'is_finish'         => 0,
                    'types'             => 2,
                    'msg'               =>'结算到平台',
                ];
                MchIdQueen::createQueen($queen);
            }else{
                Bank::dueMoney($store->member_miniapp_id,$store->manage_uid,$prorateMoney);
            }
            StoreBill::add(['miniapp_id' => $store->member_miniapp_id,'uid' => $store->manage_uid,'store_id' => $store->id,'pay_uid' => $order['user_id'],'money' => $prorateMoney],'优选下单结算到商户号');            //商家账单
            BankBill::add(['miniapp_id' => $store->member_miniapp_id,'uid' => $store->manage_uid,'store_id' => $store->id,'pay_uid' => $order['user_id'],'money' => $prorateMoney],'优选下单结算到商户号');             //个人账单
            model('StoreStats')->payment($store, $prorateMoney);  //增加统计
        }
    }
}
